import { DataTypes } from 'sequelize'

import Seq from '../config/sequelize.ts'
import CarpoolType from './carpooltype.model.ts'

// 拼车成员信息表
const CarpoolMember = Seq.define(
  'carpool_member',
  {
    carpool_member_id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      allowNull: false,
      unique: true,
      comment: '拼车成员ID',
    },
    member_name: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: '拼车人名',
    },
    start_date: {
      type: DataTypes.DATE,
      comment: '开始日期',
    },
    end_date: {
      type: DataTypes.DATE,
      comment: '结束日期',
    },
    email: {
      type: DataTypes.STRING,
      comment: '邮箱',
    },
    expire: {
      type: DataTypes.CHAR(1),
      defaultValue: '1',
      comment: '过期状态(0过期,1未过期)',
    },
    remark: {
      type: DataTypes.STRING,
      comment: '备注',
    },
    carpool_type: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: '拼车类型',
      references: {
        model: CarpoolType,
        key: 'carpool_type',
        onDelete: 'CASCADE',
      },
    },
  },
  {
    // 防止表名变成复数
    freezeTableName: true,
  },
)

// CarpoolMember.sync({ alter: true })
// Table.sync({ force: true })

CarpoolMember.belongsTo(CarpoolType, {
  foreignKey: 'carpool_type',
  targetKey: 'carpool_type',
  as: 'carpoolTypeInfo',
})
CarpoolType.hasMany(CarpoolMember, {
  foreignKey: 'carpool_type',
  sourceKey: 'carpool_type',
  as: 'carpoolMembers',
})

export default CarpoolMember
